package com.wei.service.impl;

import com.wei.dao.ProducrRepository;
import com.wei.pojo.BaseResult;
import com.wei.pojo.Product;
import com.wei.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ProductServiceImpl implements ProductService {



    @Autowired
    ProducrRepository producrRepository;
    @Override
    public BaseResult findAll(Integer page,Integer size) {
        PageRequest of = PageRequest.of(page - 1, size);

        Page<Product> all = producrRepository.findAll(of);
        BaseResult baseResult = new BaseResult();
        baseResult.setData(all.getContent());
        baseResult.setTotal(all.getTotalElements());
        return baseResult;
    }

    @Override
    public Product findById(Integer id) {
        //这的问题
        Optional<Product> byId = producrRepository.findById(id);
        if (byId.isPresent()){

            Product product = byId.get();

            return product;
        }
        return null;
    }

    @Override
    public int deletrById(Integer id) {
        producrRepository.deleteById(id);
        return 0;
    }

    @Override
    public int saveOrUpdate(Product product) {
        producrRepository.saveAndFlush(product);
        return 0;
    }
}
